package easy;

/**
 * @author admin
 * 1859. 将句子排序
 * 解题思路：将字符串拆分为一个个单词存放到s1数组，再new一个数组s2，单词中最后一个数字为下标，截取单词并按下标存放到s2数组，遍历将s2的单词拼接再去掉结尾空格
 */
public class LeeCode1859 {

    public String sortSentence(String s) {

        String res = "";
        String[] s1 = s.split(" ");

        String[] s2 = new String[10];
        for (int i = 0; i < s1.length; i++) {
//            减0是为了将char型转为int型
            s2[s1[i].charAt(s1[i].length() - 1) - '0'] = s1[i].substring(0,s1[i].length() - 1) + " ";
        }
        for (int i = 1; i <= s1.length; i++) {
            res = res + s2[i];
        }
        return res.trim();
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode1859().sortSentence("is2 sentence4 This1 a3"));
    }
}
